package events;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import blog.BlogHome;
import db.BeanUtilities;
import db.Blog;
import db.Events;
import db.HibernateUtil;

import utilities.ServletUtilities;

/**
 * Servlet implementation class Event
 */
@WebServlet("/Event")
public class EventsAdmin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EventsAdmin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Boolean isAdmin = ServletUtilities.isAdmin(request);
		if (isAdmin) {
			if(request.getParameter("idDelete") != null) {
				doDelete(request, response, Integer.parseInt(request.getParameter("idDelete")));
			} else if(request.getParameter("idEdit") != null) {
				doEdit(request, response, Integer.parseInt(request.getParameter("idEdit")));
			} else {
				response.sendRedirect("/runWithJava/admin_events.jsp");
			}
		} else {
			HttpSession session = request.getSession(true);
			session.setAttribute("error", "Dostep zabrioniony");
			response.sendRedirect("/runWithJava/");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Boolean isAdmin = ServletUtilities.isAdmin(request);
		if (isAdmin) {
			Events event = new Events();
			String red = "/runWithJava/admin_ev.jsp";
			//***********zeby Balowi error nie lecial
			if(request.getParameter("data").matches("^[0-9]{4}[-](([1][0-2])|[1-9])[-][0-9]{1,2}$") && request.getParameter("time").matches("^[0-2]?[0-9][:][0-5][0-9]$")) {
				BeanUtilities.populateBean(event, request.getParameterMap(), request, response, red);

			    HibernateUtil<Events> hu = new HibernateUtil<Events>();
			    try{
			    	hu.create(event);
			    	response.sendRedirect("/runWithJava/admin_events.jsp");
			    } catch(Exception e) {
			    	  System.out.println(e);
				      HttpSession session = request.getSession(true);
				      session.setAttribute("error", e.getMessage());
				      session.setAttribute("errorObject", event);
				      response.sendRedirect("/runWithJava/admin_ev.jsp");      
			    }
			} else {//***********zeby Balowi error nie lecial
			    response.sendRedirect("/runWithJava/ErrorTest.jsp");
			}

			

		} else {
			response.sendError(500);
		} 
	}
	
	protected void doDelete(HttpServletRequest request, HttpServletResponse response, int id) throws ServletException, IOException {
		EventsHome.removeEvent(id);
		response.sendRedirect("/runWithJava/admin_events.jsp");
	}
	
	protected void doEdit(HttpServletRequest request, HttpServletResponse response, int id) throws ServletException, IOException {
		Events event = new Events();
		BeanUtilities.populateBean(event, request.getParameterMap());
		EventsHome.editEvent(event, id);
		response.sendRedirect("/runWithJava/admin_events.jsp");
	}

}
